package com.laijiale.controller;

import cn.hutool.core.bean.BeanUtil;
import com.laijiale.Service.SchoolService;
import com.laijiale.dto.*;
import com.laijiale.entities.Room;
import com.laijiale.entities.School;
import com.laijiale.excel.RoomExcel;
import com.laijiale.excel.SchoolExcel;
import com.laijiale.result.Result;
import com.laijiale.result.ResultCode;
import com.laijiale.util.EasyExcelUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * @author laijiale
 * @date 2024年07月28日 11:59
 */
@Tag(name = "学校控制器")
@RestController
@RequestMapping("/api/v1/school")
public class SchoolController {
    @Autowired
    private SchoolService schoolService;
    @Operation(summary = "添加一条记录")
    @PostMapping("save")
    public Object save(@RequestBody  @Validated SchoolInsertDTO schoolInsertDTO){

        return schoolService.insert(schoolInsertDTO);
    }
    @Operation(summary = "查询一条记录")
    @GetMapping("getInfo/{id}")
    public Object getInfo(@PathVariable Long id){
        return schoolService.selectById(id);
    }
    @Operation(summary = "修改一条记录")
    @PostMapping("update")
    public Object update(@RequestBody @Validated SchoolUpdateDTO schoolUpdateDTO){

        return schoolService.updateById(schoolUpdateDTO);

    }
    @Operation(summary = "删除一条记录")
    @PostMapping("remove/{id}")
    public Object remove(@PathVariable Long id){

        return schoolService.logicDeleteById(id);

    }
    @Operation(summary = "批量删除记录")
    @PostMapping("removeBatch")
    public Object removeBatch(@RequestParam List<Long> ids){

        return schoolService.logicDeleteByIds(ids);

    }
    @Operation(summary = "全查询记录")
    @GetMapping("list")
    public Object list(){

        return schoolService.list();

    }
    @Operation(summary = "分页查询记录")
    @PostMapping("page")
    public Object page(@RequestBody  @Validated PageDTO pageDTO){

        return schoolService.page(pageDTO);

    }

    @SneakyThrows
    @GetMapping("/excel")
    public void excel(HttpServletResponse resp) {
        List<SchoolExcel> schoolExcels = new ArrayList<>();
        List<School> schools = schoolService.list();
        for (School school : schools) {
            SchoolExcel schoolExcel = BeanUtil.copyProperties(school, SchoolExcel.class);
            schoolExcels.add(schoolExcel);
        }
        EasyExcelUtil.download(resp, "学校统计表.xlsx", "sheet01",
                SchoolExcel.class, schoolExcels);
    }
    @Operation(summary = "按学校名搜索")
    @PostMapping("pageLikeTitle")
    public Object pageLikeTitle(@RequestBody @Validated SchoolPageDTO dto){
        return schoolService.pageLikeTitle(dto);
    }


}
